<template>
  <BasicModal v-bind="$attrs" @register="registerModal" destroyOnClose :title="title" :width="896" :height="450" @ok="handleSubmit">
      <BasicForm @register="registerForm">
      </BasicForm>
    <template #appendFooter>
      <a-button type="primary"  @click="handleDel" v-if="isUpdate" >
        删除
      </a-button>
    </template>
  </BasicModal>
</template>

<script lang="ts" setup>
    import {ref, computed, unref} from 'vue';
    import {BasicModal, useModalInner} from '/@/components/Modal';
    import {BasicForm, useForm} from '/@/components/Form/index';
    import {formSchema} from '../SsHoliday.data';
    import {saveOrUpdate,deleteOne} from '../SsHoliday.api';
    // Emits声明
    const emit = defineEmits(['register','success']);
    const isUpdate = ref(true);
    //表单配置
    const [registerForm, {setProps,resetFields, setFieldsValue, validate}] = useForm({
        //labelWidth: 150,
        schemas: formSchema,
        showActionButtonGroup: false,
        baseColProps: {span: 12},
    });
    let handleSubmit1 = null;
    //表单赋值
    const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
        //重置表单
        if(data.record.startTime && isUpdate) {
          let timePeriod = data.record.startTime + ',' + data.record.endTime
          data.record.timePeriod=timePeriod
        }else {
          data.record.timePeriod='00:00:00,23:59:59'
        }
        
        await resetFields();
        setModalProps({confirmLoading: false,showCancelBtn:!!data?.showFooter,showOkBtn:!!data?.showFooter});
        isUpdate.value = !!data?.isUpdate;
      handleSubmit1 = data.handleSubmit;
        if (unref(isUpdate)) {
            //表单赋值
            await setFieldsValue({
                ...data.record,
            });

        }else{
          await setFieldsValue({
            ...data.record,
          });
        }
        // 隐藏底部时禁用整个表单
       setProps({ disabled: !data?.showFooter })
    });
    //设置标题
    const title = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
    //表单提交事件
    async function handleSubmit(v) {
        try {
            let values = await validate();
            setModalProps({confirmLoading: true});
            //提交表单

            let arr = values.timePeriod.split(',');
            values.startTime=arr[0];
            values.endTime=arr[1];
            await saveOrUpdate(values, isUpdate.value);

            //关闭弹窗
            closeModal();

          console.log("1111")
            //刷新列表
          if(handleSubmit1) {
            console.log("kdfjdkf")
            handleSubmit1();
          }
        } finally {
            setModalProps({confirmLoading: false});
        }
    }

    function handleSuccess(){
    }
    async function handleDel(){
        let values = await validate();
        await deleteOne({id: values.id}, handleSuccess);
      //关闭弹窗
      closeModal();
      if(handleSubmit1) {
        handleSubmit1();
      }
    }
</script>

<style lang="less" scoped>
	/** 时间和数字输入框样式 */
  :deep(.ant-input-number){
		width: 100%
	}

	:deep(.ant-calendar-picker){
		width: 100%
	}
</style>
